#include "ctitlebar.h"
//#include "qss.h"
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QDebug>
#include <windows.h>
#include <qt_windows.h>

CTitleBar::CTitleBar(QWidget *parent) : QWidget(parent)
{
    initUI();
}

void CTitleBar::initUI()
{
    //禁止父窗口影响子窗口样式,必须加上，否则样式不会起作用
    setAttribute(Qt::WA_StyledBackground);

    this->setFixedHeight(32 + 10);
    this->setStyleSheet("background-color:rgb(54,54,54)");

    _plogoLabel = new QLabel(this);
    _plogoLabel->setFixedSize(32,32);
    _plogoLabel->setStyleSheet(
            "background-image:url(:/resources/logo.png);border:none");

    _ptitleTextLabel = new QLabel(this);
    _ptitleTextLabel->setText("卷饼影音");
    _ptitleTextLabel->setFixedWidth(120);
    _ptitleTextLabel->setStyleSheet(
            "QLabel{font-family: Microsoft YaHei;"
                "font-size:18px;"
                "color:#BDC8E2;background-color:rgb(54,54,54);}");

    _psetButton = new QPushButton(this);
    _psetButton->setFixedSize(32, 32);
    _psetButton->setStyleSheet(
            "QPushButton{background-image:url(:/resources/titlebar/set.svg);border:none}"
            "QPushButton:hover{"
                    "background-color:rgb(99, 99, 99);"
                    "background-image:url(:/resources/titlebar/set_hover.svg);border:none;}");

    _pminButton = new QPushButton(this);
    _pminButton->setFixedSize(32, 32);
    _pminButton->setStyleSheet(
            "QPushButton{background-image:url(:/resources/titlebar/min.svg);border:none}"
            "QPushButton:hover{"
                    "background-color:rgb(99, 99, 99);"
                    "background-image:url(:/resources/titlebar/min_hover.svg);border:none;}");

    _pmaxButton = new QPushButton(this);
    _pmaxButton->setFixedSize(32, 32);
    _pmaxButton->setStyleSheet(
            "QPushButton{background-image:url(:/resources/titlebar/normal.svg);border:none}"
            "QPushButton:hover{"
                    "background-color:rgb(99, 99, 99);"
                    "background-image:url(:/resources/titlebar/normal_hover.svg);border:none;}");

    _pcloseButton = new QPushButton(this);
    _pcloseButton->setFixedSize(32, 32);
    _pcloseButton->setStyleSheet(
            "QPushButton{background-image:url(:/resources/titlebar/close.svg);border:none}"
            "QPushButton:hover{"
                    "background-color:rgb(99, 99, 99);"
                    "background-image:url(:/resources/titlebar/close_hover.svg);border:none;}");

    QHBoxLayout * phLayout = new QHBoxLayout(this);
    phLayout->addWidget(_plogoLabel);
    phLayout->addWidget(_ptitleTextLabel);
    phLayout->addStretch();//添加一个弹簧

    phLayout->addWidget(_psetButton);

//  QSpacerItem* pItem1 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem1);

    phLayout->addWidget(_pminButton);

//  QSpacerItem* pItem2 = new QSpacerItem(18, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem2);

    phLayout->addWidget(_pmaxButton);

//  QSpacerItem* pItem3 = new QSpacerItem(18, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem3);

    phLayout->addWidget(_pcloseButton);
    phLayout->setContentsMargins(5,5,5,5);
    //this->setLayout(phLayout);

    connect(_pminButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
    connect(_pmaxButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
    connect(_pcloseButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
}


void CTitleBar::mousePressEvent(QMouseEvent* event)
{
    if (ReleaseCapture())
    {
        QWidget* pWindow = this->window();//返回指向标题栏所属顶层窗口的指针。
        if (pWindow->isTopLevel()) //判断该窗口是否为顶层窗口（即独立窗口）
        {
            SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
    QWidget::mousePressEvent(event);
}

void CTitleBar::updateMaxButtonIcon(bool isFullScreen){
    if(isFullScreen){
        _pmaxButton->setStyleSheet(
                "QPushButton{"
                "   background-image: url(:/resources/titlebar/max.svg);"
                "   background-repeat: no-repeat;"
                "   background-position: center;"
                "   background-size: contain;"  // 保持宽高比，完整显示图片
                "   border: none;"
                "}"
                "QPushButton:hover{"
                "   background-color: rgb(99, 99, 99);"
                "   background-image: url(:/resources/titlebar/max_hover.svg);"
                "   background-repeat: no-repeat;"
                "   background-position: center;"
                "   background-size: contain;"  // 保持宽高比，完整显示图片
                "   border: none;"
                "}"
            );
    }else{
        _pmaxButton->setStyleSheet(
                "QPushButton{background-image:url(:/resources/titlebar/normal.svg);border:none}"
                "QPushButton:hover{"
                        "background-color:rgb(99, 99, 99);"
                        "background-image:url(:/resources/titlebar/normal_hover.svg);border:none;}");
    }

}


void CTitleBar::onClickedSlot()
{
    QPushButton * pbtn = qobject_cast<QPushButton*>(sender());
    QWidget * pwindow = this->window();
    if(pbtn == _pminButton) {
        pwindow->showMinimized();
    } else if(pbtn == _pmaxButton) {
        if(pwindow->isFullScreen()) {
            pwindow->showNormal();
            updateMaxButtonIcon(false);
        } else  {
            pwindow->showFullScreen();
            updateMaxButtonIcon(true);
        }
    } else if(pbtn == _pcloseButton) {
        emit sigClose();//发射信号，通知父窗口关闭
    }
}

void CTitleBar::mouseDoubleClickEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton){
        emit sigTitleFull();
    }else if(event->button() == Qt::RightButton){
        qDebug() << __LINE__ ;
    }

    QWidget::mouseDoubleClickEvent(event);
}
